package com.letv.android.client.episode.parser;

import com.letv.core.bean.CmsBean;
import com.letv.core.bean.RecAlbumBean;
import com.letv.core.bean.RecommendBean;
import com.letv.core.bean.RelateBean;
import com.letv.core.bean.RelateBean.PicCollections;
import com.letv.core.bean.RelateSetBean;
import com.letv.core.bean.RelateSetBean.Title;
import com.letv.core.bean.VideoBean;
import com.letv.core.bean.VideoRelatedListBean;
import com.letv.core.parser.LetvMobileParser;
import com.letv.core.utils.LogInfo;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import com.letv.android.client.episode.bean.TabRelateListBean.Title;

public class RelateParser extends LetvMobileParser<VideoRelatedListBean> {

	private static final String TAG = RelateParser.class.getSimpleName();
	
	private static final String VID = "vid";
	private static final String PID = "pid";
	private static final String CID = "cid";
	private static final String NAMECN = "nameCn";
	private static final String SUBTITLE = "subTitle";
	private static final String SINGER = "singer";
	private static final String GUEST = "guest";
	private static final String TYPE = "type";
	private static final String BTIME = "btime";
	private static final String ETIME = "etime";
	private static final String DURATION = "duration";
	private static final String MID = "mid";
	private static final String EPISODE = "episode";
	private static final String PORDER = "porder";
	private static final String PAY = "pay";
	private static final String DOWNLOAD = "download";
	private static final String PICALL = "picAll";
	private static final String PIC120_90 = "120*90";
	private static final String PIC200_150 = "200*150";
	private static final String PIC320_200 = "320*200";
	private static final String PLAY = "play";
	private static final String PLAYCOUNT = "playCount";	
	private static final String JUMP = "jump";
	private static final String JUMPLINK = "jumplink";
	private static final String BRLIST = "brList";
	private static final String VIDEOTYPEKEY = "videoTypeKey";
	private static final String VIDEOTYPE = "videoType";
	private static final String CONTROLAREAS = "controlAreas";
	private static final String DISABLETYPE = "disableType";
	private static final String WATCHINGFOCUS = "watchingFocus";
	
	private static final String ID = "id";
	private static final String DESC = "desc";
	private static final String PICURL = "pic";
	private static final String TIMEDOT = "time";
	
	/**
	 * 正片推荐 往期回顾
	 */
	public void parseRecAlbumInfo(JSONObject data, VideoRelatedListBean videoRelatedList){
		try {
			if (has(data, "recAlbumInfo") && getJSONObject(data, "recAlbumInfo").length() != 0){
				videoRelatedList.recAlbumInfo = new RecAlbumBean();
				JSONObject recAlbumInfo = getJSONObject(data, "recAlbumInfo");
				
				videoRelatedList.recAlbumInfo.setAlbumType(getString(recAlbumInfo, "albumType"));
				videoRelatedList.recAlbumInfo.setAlbumTypeKey(getString(recAlbumInfo, "albumTypeKey"));
				videoRelatedList.recAlbumInfo.setAlias(getString(recAlbumInfo, "alias"));
				videoRelatedList.recAlbumInfo.setArea(getString(recAlbumInfo, "area"));
				videoRelatedList.recAlbumInfo.setAt(getString(recAlbumInfo, "at"));
				videoRelatedList.recAlbumInfo.setCast(getString(recAlbumInfo, "cast"));
				videoRelatedList.recAlbumInfo.setCid(getString(recAlbumInfo, "cid"));
				videoRelatedList.recAlbumInfo.setCompere(getString(recAlbumInfo, "compere"));
				videoRelatedList.recAlbumInfo.setControlAreas(getString(recAlbumInfo, "controlAreas"));
				videoRelatedList.recAlbumInfo.setDescription(getString(recAlbumInfo, "description"));
				videoRelatedList.recAlbumInfo.setDirectory(getString(recAlbumInfo, "directory"));
				videoRelatedList.recAlbumInfo.setDisableType(getString(recAlbumInfo, "disableType"));
				videoRelatedList.recAlbumInfo.setDownload(getString(recAlbumInfo, "download"));
				videoRelatedList.recAlbumInfo.setDub(getString(recAlbumInfo, "dub"));
				videoRelatedList.recAlbumInfo.setDuration(getString(recAlbumInfo, "duration"));
				videoRelatedList.recAlbumInfo.setEpisode(getString(recAlbumInfo, "episode"));
				videoRelatedList.recAlbumInfo.setFitAge(getString(recAlbumInfo, "fitAge"));
				videoRelatedList.recAlbumInfo.setInstructor(getString(recAlbumInfo, "instructor"));
				videoRelatedList.recAlbumInfo.setIsEnd(getString(recAlbumInfo, "isEnd"));
				videoRelatedList.recAlbumInfo.setIsHomemade(getString(recAlbumInfo, "isHomemade"));
				videoRelatedList.recAlbumInfo.setJump(getString(recAlbumInfo, "jump"));
				videoRelatedList.recAlbumInfo.setLanguage(getString(recAlbumInfo, "language"));
				videoRelatedList.recAlbumInfo.setNameCn(getString(recAlbumInfo, "nameCn"));
				videoRelatedList.recAlbumInfo.setNowEpisodes(getString(recAlbumInfo, "nowEpisodes"));
				videoRelatedList.recAlbumInfo.setOriginator(getString(recAlbumInfo, "originator"));
				videoRelatedList.recAlbumInfo.setPay(getString(recAlbumInfo, "pay"));
				videoRelatedList.recAlbumInfo.setPid(getString(recAlbumInfo, "pid"));
				
				if (has(recAlbumInfo, "picCollections")){
					JSONObject picCollections = recAlbumInfo.getJSONObject("picCollections");
					videoRelatedList.recAlbumInfo.setPic320(getString(picCollections, "320*200"));
				}
				
				videoRelatedList.recAlbumInfo.setPlayStatus(getString(recAlbumInfo, "playStatus"));
				videoRelatedList.recAlbumInfo.setPlayTv(getString(recAlbumInfo, "playTv"));
				videoRelatedList.recAlbumInfo.setrCompany(getString(recAlbumInfo, "rCompany"));
				videoRelatedList.recAlbumInfo.setRelationAlbumId(getString(recAlbumInfo, "relationAlbumId"));
				videoRelatedList.recAlbumInfo.setReleaseDate(getString(recAlbumInfo, "releaseDate"));
				videoRelatedList.recAlbumInfo.setPlatformVideoInfo(getString(recAlbumInfo, "platformVideoInfo"));
				videoRelatedList.recAlbumInfo.setPlatformVideoNum(getString(recAlbumInfo, "platformVideoNum"));
				videoRelatedList.recAlbumInfo.setRelationId(getString(recAlbumInfo, "relationId"));
				videoRelatedList.recAlbumInfo.setStyle(getString(recAlbumInfo, "style"));
				videoRelatedList.recAlbumInfo.setType(getString(recAlbumInfo, "type"));
				videoRelatedList.recAlbumInfo.setScore(getString(recAlbumInfo, "score"));
				videoRelatedList.recAlbumInfo.setSubTitle(getString(recAlbumInfo, "subTitle"));
				videoRelatedList.recAlbumInfo.setSubcid(getString(recAlbumInfo, "scsubcidore"));
				videoRelatedList.recAlbumInfo.setSupervise(getString(recAlbumInfo, "supervise"));
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}	
	
	@Override
	public VideoRelatedListBean parse(JSONObject data) throws Exception {
		LogInfo.log(TAG, "RelateParser data :" + data);
		VideoRelatedListBean videoRelatedList = new VideoRelatedListBean();
		ArrayList<RelateSetBean> cmsArray = null;
		
		/**
		 * cms data
		 */
		if (data.has("cmsdata") && data.getJSONArray("cmsdata").length() > 0){
			JSONArray cmsdata = data.getJSONArray("cmsdata");
			cmsArray = new ArrayList<RelateSetBean>();
			for (int i = 0; i < cmsdata.length(); i++){
				RelateSetBean relatedBean = new RelateSetBean();
				relatedBean.type = VideoRelatedListBean.CMS_CONTENT_TYPE;
				CmsBean cms = new CmsBean();
				relatedBean.cms = cms;
				JSONObject objs = cmsdata.getJSONObject(i);
				
				cms.setCmsid(getString(objs, "cmsid"));
				cms.setAt(getInt(objs, "at"));
				cms.setIs_rec(getBoolean(objs, "is_rec"));
				cms.setMobilePic(getString(objs, "mobilePic"));
				cms.setNameCn(getString(objs, "nameCn"));
				cms.setPadPic(getString(objs, "padPic"));
				cms.setSubTitle(getString(objs, "subTitle"));
				cms.setWebViewUrl(getString(objs, "webViewUrl"));
				cms.setPid(getInt(objs, "pid"));
				cms.setVid(getInt(objs, "vid"));
				cms.setZid(getString(objs, "zid"));
				cms.setNameCn(getString(objs, "nameCn"));
				cms.setSubTitle(getString(objs, "subTitle"));
				cms.setCid(getInt(objs, "cid"));
				cms.setType(getInt(objs, "type"));
				cms.setAlbumType(getString(objs, "albumType"));
	            cms.setVideoType(getString(objs, "videoType"));
	            cms.setVarietyShow(getInt(objs, "varietyShow") == 1);
	            cms.setAt(getInt(objs, "at"));
	            cms.setEpisode(getString(objs, "episode"));
	            cms.setNowEpisodes(getString(objs, "nowEpisodes"));
	            cms.setIsEnd(getInt(objs, "isEnd"));
	            cms.setPay(getInt(objs, "pay"));
				cms.setTag(getString(objs, "ledimVedioTagBean"));
				cms.setStreamCode(getString(objs, "streamCode"));
	            cms.setWebUrl(getString(objs, "webUrl"));
	            cms.setStreamUrl(getString(objs, "streamUrl"));
	            cms.setTm(getString(objs, "tm"));
	            cms.setDuration(getString(objs, "duration"));
	            cms.setMobilePic(getString(objs, "mobilePic"));
	            if (has(objs, "singer")) {
	            	cms.setSinger(getString(objs, "singer"));
	            }
				cmsArray.add(relatedBean);
			}
		}
		
		/**
		 * 相关数据
		 */
		parseRelateAlbums(data, videoRelatedList);
		
		/**
		 * 猜你喜欢
		 */
		if (has(data, "recData")){
			JSONArray recData = getJSONArray(data, "recData");
			
			if ((recData.length() > 0 || (cmsArray != null && cmsArray.size() > 0))){
				RelateSetBean relatedBean = new RelateSetBean();
				relatedBean.type = VideoRelatedListBean.RECT_TITLE_TYPE;
				Title rectTitle = new Title();
				rectTitle.titleName = "猜你喜欢";
				if (cmsArray != null){
					rectTitle.count = recData.length() + cmsArray.size();
				}else{
					rectTitle.count = recData.length();
				}
				relatedBean.title = rectTitle;
				videoRelatedList.add(relatedBean);
			}
			
			if (cmsArray != null && cmsArray.size() > 0){ // cms data 添加到猜你喜欢数据前面
				videoRelatedList.addAll(cmsArray);
			}			
			
			//解析单视频中正在播放的视频信息
            VideoBean tabVideoInfoBean = parse2VideoPlayerLibs(data);
            if (tabVideoInfoBean != null) {                
                RelateSetBean relatedBean = new RelateSetBean();
                relatedBean.type = VideoRelatedListBean.SELF_VIDEO_TYPE;
                relatedBean.tabVideoInfoBean = tabVideoInfoBean;
                videoRelatedList.add(relatedBean);
            }

			for (int i = 0; i < recData.length(); i++){
				RelateSetBean relatedBean = new RelateSetBean();
				RecommendBean rect = new RecommendBean();
				relatedBean.rect = rect;
				relatedBean.type = VideoRelatedListBean.RECT_CONTENT_TYPE;
				
				JSONObject rectJsonObj = recData.getJSONObject(i);
				
				rect.area = rectJsonObj.getString("area");
				rect.bucket = rectJsonObj.getString("bucket");
				rect.director = rectJsonObj.getString("director");
				rect.episode = rectJsonObj.getInt("episode");
				rect.isEnd = rectJsonObj.getInt("isEnd");
				rect.jump = rectJsonObj.getInt("jump");
				rect.nowEpisodes = rectJsonObj.getInt("nowEpisodes");
				rect.pay = getString(rectJsonObj, "pay");
				rect.pic320 = getString(rectJsonObj, "pic320*200");
				rect.picHT = getString(rectJsonObj, "picHT");
				rect.picST = getString(rectJsonObj, "picST");
				rect.pid = getInt(rectJsonObj, "pid");
				rect.pidname = getString(rectJsonObj, "pidname");
				rect.playCount = getString(rectJsonObj, "playCount");
				rect.reid = getString(rectJsonObj, "reid");
				rect.starring = getString(rectJsonObj, "starring");
				rect.subname = getString(rectJsonObj, "subname");
				rect.title = getString(rectJsonObj, "title");
				rect.type = getInt(rectJsonObj, "type");
				rect.id = getInt(rectJsonObj, "vid");
				rect.cid = getInt(rectJsonObj, "cid");
				rect.score = getString(rectJsonObj, "score");
				rect.singer = getString(rectJsonObj, "singer");
				rect.duration = getString(rectJsonObj, "duration");
				
				rect.subTitle = getString(rectJsonObj, "subTitle");
				rect.releaseDate = getString(rectJsonObj, "releaseDate");
				rect.subCategory = getString(rectJsonObj, "subCategory");
				rect.guest = getString(rectJsonObj, "guest");
				rect.videoType = getString(rectJsonObj, "videoType");
				rect.style = getString(rectJsonObj, "style");
				rect.pidsubtitle = getString(rectJsonObj, "pidsubtitle");
				rect.dataArea = getString(rectJsonObj, "dataArea");
				rect.cornerMark = getString(rectJsonObj, "cornerMark");
				if (null == tabVideoInfoBean || (tabVideoInfoBean != null && tabVideoInfoBean.getVid() != rect.id)) {
					videoRelatedList.add(relatedBean);
				}
			}
		}
		
		return videoRelatedList;
	}
	
	private ArrayList<String> parse2BrList(JSONArray data) throws JSONException {
		if (data == null) {
			return null;
		}
		
		ArrayList<String> brListArray = new ArrayList<String>();
		int length = data.length();
		for (int i = 0; i < length; i++) {
			brListArray.add((String)data.get(i));
		}
		return brListArray;
	}
	
	/*
	 * 解析单视频正在播放的视频info
	 */
	private VideoBean parse2VideoPlayerLibs(JSONObject data) throws JSONException {
		if (data == null) {
			return null;
		}
		if (!has(data, "selfVideo")){
			return null;
		}
		
		data = data.getJSONObject("selfVideo");
		
		VideoBean tabVideoInfoBean = new VideoBean();
		tabVideoInfoBean.setVid(getLong(data, VID));
		tabVideoInfoBean.setPid(getLong(data, PID));
		tabVideoInfoBean.setCid(getInt(data, CID));
		tabVideoInfoBean.setNameCn(getString(data, NAMECN));
		tabVideoInfoBean.setSubTitle(getString(data, SUBTITLE));
		tabVideoInfoBean.setSinger(getString(data, SINGER));
		tabVideoInfoBean.setGuest(getString(data, GUEST));
		tabVideoInfoBean.setType(getInt(data, TYPE));
		tabVideoInfoBean.setBtime(getLong(data, BTIME));
		tabVideoInfoBean.setEtime(getLong(data, ETIME));
		tabVideoInfoBean.setDuration(getLong(data, DURATION));
		tabVideoInfoBean.setMid(getString(data, MID));
		tabVideoInfoBean.setEpisode(getString(data, EPISODE));
		tabVideoInfoBean.setPorder(getString(data, PORDER));
		tabVideoInfoBean.setPay(getInt(data, PAY));
		tabVideoInfoBean.setDownload(getInt(data, DOWNLOAD));
//		tabVideoInfoBean.setp(getString(data, PICALL));
		
		tabVideoInfoBean.setPic(getString(getJSONObject(data, PICALL), PIC120_90));
		tabVideoInfoBean.setPic300(getString(getJSONObject(data, PICALL), PIC200_150));
		tabVideoInfoBean.setPic320(getString(getJSONObject(data, PICALL), PIC320_200));
		tabVideoInfoBean.setPlay(getInt(data, PLAY));
		tabVideoInfoBean.setJump(getInt(data, JUMP));
		tabVideoInfoBean.setJumpLink(getString(data, JUMPLINK));
		tabVideoInfoBean.setBrList(getString(data, BRLIST));	//parse2BrList(getJSONArray(data, BRLIST))
		tabVideoInfoBean.setVideoTypeKey(getString(data, VIDEOTYPEKEY));
		tabVideoInfoBean.setVideoType(getString(data, VIDEOTYPE));
		tabVideoInfoBean.setControlAreas(getString(data, CONTROLAREAS));
		tabVideoInfoBean.setDisableType(getInt(data, DISABLETYPE));
		tabVideoInfoBean.setPlayCount(getLong(data, PLAYCOUNT));
		tabVideoInfoBean.setType(getInt(data, "type"));
		tabVideoInfoBean.setCornerMark(getString(data, "cornerMark"));
		tabVideoInfoBean.setReleaseDate(getString(data, "releaseDate"));
		tabVideoInfoBean.setSubCategory(getString(data, "subCategory"));
		tabVideoInfoBean.setStyle(getString(data, "style"));
		tabVideoInfoBean.setArea(getString(data, "dataArea"));
		
		
		JSONArray watchFocusArray = getJSONArray(data, WATCHINGFOCUS); 
		if (watchFocusArray != null) {
			tabVideoInfoBean.setWatchingFocusList(parse2WatchingFocusBeanList(watchFocusArray));
		}
		return tabVideoInfoBean;
	}
	
	private ArrayList<VideoBean.WatchingFocusItem> parse2WatchingFocusBeanList(JSONArray data) throws JSONException {
		if (data == null) {
			return null;
		}
		ArrayList<VideoBean.WatchingFocusItem> tabWatchingFocusBeanList = new ArrayList<VideoBean.WatchingFocusItem>();
		int length = data.length();
		VideoBean.WatchingFocusItem tabWatchingFocusBean;
		for (int i = 0; i < length; i++) {
			JSONObject object = (JSONObject) data.get(i);
			tabWatchingFocusBean = new VideoBean.WatchingFocusItem();
			tabWatchingFocusBean.setId(getInt(object, ID));
			tabWatchingFocusBean.setDesc(getString(object, DESC));
			tabWatchingFocusBean.setPicUrl(getString(object, PICURL));
			tabWatchingFocusBean.setTimeDot(getString(object, TIMEDOT));			
			tabWatchingFocusBeanList.add(tabWatchingFocusBean);
		}		
		return tabWatchingFocusBeanList;		
	}
	
	private void parseRelateAlbums(JSONObject data, VideoRelatedListBean videoRelatedList) throws Exception{
		if (has(data, "relateAlbums")){
			JSONArray relateAlbums = getJSONArray(data, "relateAlbums");
			//只有相关数据没有猜你喜欢数据 标题不显示
			if (has(data, "recData") && getJSONArray(data, "recData").length() > 0 && relateAlbums.length() > 0){
				RelateSetBean relatedBean = new RelateSetBean();
				relatedBean.type = VideoRelatedListBean.RELATE_TITLE_TYPE;
				Title relateTitle = new Title();
				relateTitle.titleName = "相关系列";
				relateTitle.count = relateAlbums.length();
				relatedBean.title = relateTitle;
				videoRelatedList.add(relatedBean);
			}
			
			for (int i = 0; i < relateAlbums.length(); i++){
				RelateSetBean relatedBean = new RelateSetBean();
				relatedBean.type = VideoRelatedListBean.RELATE_CONTENT_TYPE;
				RelateBean relate = new RelateBean();
				relatedBean.relate = relate;
				videoRelatedList.add(relatedBean);
				
				JSONObject relateObj = relateAlbums.getJSONObject(i);
				
				relate.albumType = getString(relateObj, "albumType");
				relate.albumTypeKey = getString(relateObj, "albumTypeKey");
				relate.alias = getString(relateObj, "alias");
				relate.area = getString(relateObj, "area");
				relate.at = getInt(relateObj, "at");
				relate.cast = getString(relateObj, "cast");
				relate.cid = getString(relateObj, "cid");
				relate.compere = getString(relateObj, "compere");
				relate.controlAreas = getString(relateObj, "controlAreas");
				relate.description = getString(relateObj, "description");
				relate.directory = getString(relateObj, "directory");
				relate.disableType = getString(relateObj, "disableType");
				relate.download = getString(relateObj, "download");
				relate.dub = getString(relateObj, "dub");
				relate.duration = getString(relateObj, "duration");
				relate.episode = getString(relateObj, "episode");
				relate.fitAge = getString(relateObj, "fitAge");
				relate.instructor = getString(relateObj, "instructor");
				relate.isEnd = getString(relateObj, "isEnd");
				relate.isHomemade = getString(relateObj, "isHomemade");
				relate.language = getString(relateObj, "language");
				relate.nameCn = getString(relateObj, "nameCn");
				relate.nowEpisodes = getString(relateObj, "nowEpisodes");
				relate.originator = getString(relateObj, "originator");
				
				JSONObject picjsonObj = getJSONObject(relateObj, "picCollections");
				if (picjsonObj != null){
					relate.picCollections = new PicCollections();
					relate.picCollections.pic150 = picjsonObj.getString("150*200");
					relate.picCollections.pic300 = picjsonObj.getString("300*300");
					relate.picCollections.pic400 = picjsonObj.getString("400*300");
					relate.picCollections.pic320 = picjsonObj.getString("320*200");
				}
				
				relate.pid = getString(relateObj, "pid");
				relate.platformVideoInfo = getString(relateObj, "platformVideoInfo");
				relate.platformVideoNum = getString(relateObj, "platformVideoNum");
				relate.play = getString(relateObj, "play");
				relate.playCount = getString(relateObj, "playCount");
				relate.playStatus = getString(relateObj, "playStatus");
				relate.playTv = getString(relateObj, "playTv");
				relate.rCompany = getString(relateObj, "rCompany");
				relate.relationAlbumId = getString(relateObj, "relationAlbumId");
				relate.relationId = getString(relateObj, "relationId");
				relate.releaseDate = getString(relateObj, "releaseDate");
				relate.school = getString(relateObj, "school");
				relate.score = getString(relateObj, "score");
				relate.starring = getString(relateObj, "starring");
				relate.style = getString(relateObj, "style");
				relate.subCategory = getString(relateObj, "subCategory");
				relate.subcid = getString(relateObj, "subcid");
				relate.subTitle = getString(relateObj, "subTitle");
				relate.supervise = getString(relateObj, "supervise");
				relate.tag = getString(relateObj, "ledimVedioTagBean");
				relate.type = getString(relateObj, "type");
				relate.pidsubtitle = getString(relateObj, "pidsubtitle");
				relate.cornerMark = getString(relateObj, "cornerMark");
			}
			
		}
	}

}
